package cn.chenyu.util;


import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.jsoup.select.Selector;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;


public class URLTest {
    private  static final Logger log  = Logger.getLogger(URLTest.class);
    
    
    public static void UrlTest(){
        try {
            //InetAddress
            // 创建一个URL实例
            URL url = new URL("http://www.imooc.com");
            System.out.println("协议：" + url.getProtocol());
            System.out.println("主机：" + url.getHost());
            System.out.println("授权：" + url.getAuthority());
            System.out.println("内容：" + url.getContent());
            System.out.println("端口：" + url.getPort());
            System.out.println("文件路径：" + url.getPath());
            System.out.println("文件名：" + url.getFile());
            System.out.println("相对路径：" + url.getRef());
            System.out.println("查询字符串：" + url.getQuery());
        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }
    @Test
    public void TbTest() throws IOException {
        String url = "https://tieba.baidu.com/f?kw=%E5%83%8F%E7%B4%A0%E7%94%BB&ie=utf-8&tab=good";
        //模拟浏览器请求1
//        doc = Jsoup.connect("http://blog.csdn.net/roy_70")
//                .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31")
//                .get();
        //模拟浏览器请求2
//        Document doc = Jsoup.connect("http://blog.csdn.net/roy_70")
//                .data("query", "Java")
//                .userAgent("Mozilla")
//                .cookie("auth", "token")
//                .timeout(3000)
//                .post();
        //请求3
        Document document = Jsoup.connect(url).get();
        Elements element = document.getElementsByTag("html");
        Element e1 = element.first();
        Elements e2 = e1.select("body");
        Elements e3 = e2.get(0).select(".t_con cleafix");
        if(e3 != null){
            for(Element e4 : e3){
                System.out.println(e4.html());
            }
        }
        System.err.println("完成");
    }
}
